Circular Queue Using Array Algorithm

The Circular Queue using Array Algorithm is a data structure that utilizes an array to implement a queue in a circular fashion. This algorithm takes advantage of the modularity concept to achieve a more efficient use of memory compared to a traditional linear queue. The main idea behind a circular queue is to treat the front and rear indices of the queue as if they were wrapping around the array, thus allowing for more efficient use of the available array space. When the rear index reaches the end of the array, it wraps around to the beginning, making the array act like a circle. In this algorithm, two indices, front and rear, are maintained to keep track of the elements in the queue. The front index points to the first element in the queue, and the rear index points to the last element. When an element is enqueued, the rear index is incremented (modulo the array size) and the element is placed at the new rear position. When an element is dequeued, the element at the front index is removed, and the front index is incremented (modulo the array size). This process ensures that there is no need to shift elements within the array as the queue grows and shrinks. To avoid the ambiguity between an empty queue and a full queue (when both front and rear indices are equal), either a separate flag or a dummy element is used to distinguish these two cases. This efficient memory utilization and constant-time operations for enqueue and dequeue make the Circular Queue using Array Algorithm an ideal choice for various applications, such as task scheduling, buffering, and simulations.
#include <iostream>
using namespace std;

int queue[10];
int front = 0;
int rear = 0;
int count = 0;

void Enque(int x)
{
	if (count == 10)
	{
		cout << "\nOverflow";
	}
	else
	{
		queue[rear] = x;
		rear = (rear + 1) % 10;
		count++;
	}
}

void Deque()
{
	if (front == rear)
	{
		cout << "\nUnderflow";
	}

	else
	{
		cout << "\n"
			 << queue[front] << " deleted";
		front = (front + 1) % 10;
		count--;
	}
}

void show()
{
	for (int i = 0; i < count; i++)
	{
		cout << queue[(i + front) % 10] << "\t";
	}
}

int main()
{
	int ch, x;
	do
	{
		cout << "\n1. Enque";
		cout << "\n2. Deque";
		cout << "\n3. Print";
		cout << "\nEnter Your Choice : ";
		cin >> ch;
		if (ch == 1)
		{
			cout << "\nInsert : ";
			cin >> x;
			Enque(x);
		}
		else if (ch == 2)
		{
			Deque();
		}
		else if (ch == 3)
		{
			show();
		}
	} while (ch != 0);

	return 0;
}

LANGUAGE:

DARK MODE: